package com.ohuang.wallpapermanager.repository

import com.ohuang.wallpapermanager.data.WallPaperInfoData
import com.ohuang.wallpapermanager.db.WallPaperConfigInfo
import com.ohuang.wallpapermanager.db.WallPaperInfoDb

class WallPaperInfoRepository(isLock: Boolean) {
    private var mData = WallPaperInfoDb(if (isLock) "lock" else "home")
    private var mConfig = WallPaperConfigInfo(if (isLock) "lock" else "home")

    fun getConfig() = mConfig

    fun clearSettingConfig(){
        mConfig.lastUpdateWallId=0L
        mConfig.lastUpdateTime=0L
        mConfig.lastUpdateTimeRandom=0L
    }

    fun getWallPaperInfoList(): MutableList<WallPaperInfoData> {
        return mData.wallPaperInfoList
    }

    fun findWallPaperInfo(id: Long): WallPaperInfoData? {
        val wallPaperInfoList = mData.wallPaperInfoList
        return wallPaperInfoList.firstOrNull { it.id == id }
    }

    fun addWallPaperInfo(wallPaperInfoData: WallPaperInfoData) {
        if (wallPaperInfoData.id == 0L) {
            wallPaperInfoData.id = System.currentTimeMillis()
        }
        val wallPaperInfoList = mData.wallPaperInfoList
        removeWallPaperInfo(wallPaperInfoData.id, wallPaperInfoList)
        wallPaperInfoList.add(0,wallPaperInfoData)
        mData.wallPaperInfoList=wallPaperInfoList
    }

    fun deleteWallPaperInfo(id: Long) {
        val wallPaperInfoList = mData.wallPaperInfoList
        removeWallPaperInfo(id, wallPaperInfoList)
        mData.wallPaperInfoList=wallPaperInfoList
    }

    private fun removeWallPaperInfo(id: Long, wallPaperInfoList: MutableList<WallPaperInfoData>) {
        wallPaperInfoList.removeIf { it.id == id }
    }
}



